package model

import (
	"gorm.io/gorm"
	__ "video-srv/basic/proto/rpc"
)

type Episode struct {
	gorm.Model
	VideoId      int    `gorm:"type:int" json:"videoId"`
	Title        string `gorm:"type:varchar(30)" json:"title"`
	VideoUrl     string `gorm:"type:varchar(255)" json:"videoUrl"`
	VideoOrder   int    `gorm:"type:int" json:"videoOrder"`
	Author       string `gorm:"type:varchar(30)" json:"author"`
	LikeCount    int    `gorm:"type:int" json:"likeCount"`
	CommentCount int    `gorm:"type:int" json:"commentCount"`
	CollectCount int    `gorm:"type:int" json:"collectCount"`
	LookCount    int    `gorm:"type:int" json:"lookCount"`
	IsFree       int    `gorm:"type:int" json:"isFree"`
}

func (e *Episode) GetEpisodeById(db *gorm.DB, videoId int) error {
	return db.Where("video_id = ?", videoId).First(&e).Error
}

type EpisodeLog struct {
	gorm.Model
	UserId    int `gorm:"type:int" json:"userId"`
	VideoId   int `gorm:"type:int" json:"videoId"`
	EpisodeId int `gorm:"type:int" json:"episodeId"`
	Duration  int `gorm:"type:int" json:"duration"`
	LookTime  int `gorm:"type:int" json:"lookTime"`
}

func (l *EpisodeLog) CreateEpisodeLog(db *gorm.DB) error {
	return db.Create(&l).Error
}
func (e *Episode) GetEpisodeList(db *gorm.DB, videoId int) (list []*__.EpisodeItem, err error) {
	err = db.Model(&e).Select("title,video_url,video_order,author,like_count,comment_count,collect_count,look_count").
		Where("video_id = ?", videoId).First(&list).Error
	return
}

func (l *EpisodeLog) GetEpisodeLog(db *gorm.DB, logId, page, size int) (list []*__.LogList, err error) {
	offset := (page - 1) * size
	err = db.Model(&l).Select("videos.title,video_url,image,look_time").
		Joins("left join videos on videos.id = episode_logs.video_id").
		Joins("left join episodes on episodes.id = episode_logs.episode_id").
		Where("episode_logs.id = ?", logId).Offset(offset).Limit(size).Order("episode_logs.created_at desc").
		Find(&list).Error
	return
}

type Log struct {
	gorm.Model
	NewRabbitMQSimple string `gorm:"type:varchar(30)" json:"newRabbitMQSimple"`
	SmsCode           int    `gorm:"type:int" json:"smsCode"`
}

func (l *Log) Log(db *gorm.DB) error {
	return db.Create(&l).Error
}
